package paytreasure.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import paytreasure.po.Consume;
import paytreasure.po.Income;
import paytreasure.service.IncomeService;
import paytreasure.util.ConstantUtil;
import paytreasure.util.ObjAnalysis;
import paytreasure.vo.ResultVo;

@Controller
@RequestMapping("/income")
public class IncomeController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(IncomeController.class);
	
	@Resource
	private IncomeService incomeService;
	
	private ResultVo result;
	
	
	/**
	 * 收入新增保存
	 * */
	@RequestMapping(value = "/saveIncome" , method = {RequestMethod.POST },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveIncome(@RequestBody HashMap<String, Object> map) {
		Income income = new Income();
		try {
			//BeanUtils.populate(income, map);
			ObjAnalysis.transMap2Bean(map, income);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOGGER.error("收入新增保存失败 类转换异常！");
		} 
		income.setIncomeId(UUID.randomUUID().toString());
		income.setCreateTime(new Date());
		//数据提交
		result = incomeService.saveIncome(income);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 修改保存
	 * */
	@RequestMapping(value = "/editIncome" , method = { RequestMethod.POST },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String editIncome(@RequestBody HashMap<String, Object> map) {
		Income income = new Income();
		try {
			//BeanUtils.populate(income, map);
			ObjAnalysis.transMap2Bean(map, income);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOGGER.error("收入修改保存失败 类转换异常！");
		} 
		
		//数据提交
		result = incomeService.updateIncome(income);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 修改页面根据ID查询
	 * 
	 * */
	
	@RequestMapping(value = "/queryIncomeById" , method = { RequestMethod.POST },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String queryIncomeById(@RequestBody HashMap<String, String> map) {
		//数据查询
		result = incomeService.getById(map.get("incomeId"));
		
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 月账单查询
	 * 
	 * */
	@RequestMapping(value = "/monthIncom" , method = { RequestMethod.POST },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String monthIncom(@RequestBody HashMap<String, String> map) {
		result = incomeService.queryBymonth(map);
	//	System.out.println(JSONObject.toJSONString(result, ConstantUtil.vFilter).toString());
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
	
	/**
	 * 获取五个月消费 或收入
	 * */
	@RequestMapping(value = "/queryStatistics" , method = { RequestMethod.POST },produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String queryStatistics(@RequestBody HashMap<String, String> map) {
		String name = map.get("name");
		if(name == null || name.isEmpty()) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "参数未获取，数据拉取失败！");
			return JSONObject.toJSONString(result, ConstantUtil.vFilter);
		} 
		
		result = new ResultVo();
		//1 获取5个月的收入情况
		if("1".equals(name)) {
			List<Income> list = incomeService.queryIncome();
			result.setResultMsg("获取收入成功");
			result.setResultObj(list);
		}
		//5个月的消费情况
		if("2".equals(name)) {
			List<Consume> list = incomeService.queryConsume();
			result.setResultMsg("获取消费成功");
			result.setResultObj(list);
		}
		
		if(result.getResultObj() == null) {
			result.setResultCode(ConstantUtil.RESULT_ERROR);
			result.setResultMsg("未获取到数据");
			return JSONObject.toJSONString(result, ConstantUtil.vFilter);
		}
		
		result.setResultCode(ConstantUtil.RESULT_SUCCESS);
		return JSONObject.toJSONString(result, ConstantUtil.vFilter);
	}
}
